﻿using System;

namespace RexToy.UI.Controls
{
    public class HPanel : Container
    {
        private uint __columns;
        public uint Columns
        {
            get { return __columns; }
        }


        private Control[][] _colctrls;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columns">only 2,3,4,5 is valid</param>
        public HPanel(uint columns)
        {
            __sizeUpdate = true;
            var arr = new string[] { "a", "b", "c", "d", "e" };
            if (columns < 2 || columns > 5)
                throw new System.RangeError("columns value not valid");
            this.__columns = columns;
            var hostClass = StringExt.Format("ui-grid-{0}", arr[columns - 2]);
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Div,
                hashName = "host",
                className = hostClass
            });

            for (var i = 0; i < columns; i++)
            {
                var childClass = StringExt.Format("ui-block-{0}", arr[i]);
                var child = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
                {
                    tag = Tag.Div,
                    className = childClass
                });
                this.__domNode.AppendChild(child);
            }

            this._colctrls = new Control[][] { };
            for (var i = 0; i < columns; i++)
            {
                this._colctrls.Push(new Control[0]);
            }
        }

        public DomNode GetColumnDomNode(int column)
        {
            return this.__domNode.GetChildren()[column];
        }

        public void SetColumnsRatio(uint[] ratios)
        {
            uint sum = 0;
            for (var i = 0; i < ratios.Length; i++)
            {
                sum += ratios[i];
            }
            var perc = new string[0];
            for (var i = 0; i < ratios.Length; i++)
            {
                var p = ratios[i] / sum * 100;
                var str = p.ToFixed(2) + "%";
                perc.Push(str);
            }
            this.SetColumnsPercent(perc);
        }

        public void SetColumnsPercent(string[] perc)
        {
            for (var i = 0; i < perc.Length; i++)
            {
                var style = this.__domNode.GetChildren()[i].GetStyle();
                style.Width = perc[i];
            }
        }

        public void AppendControl(uint col, Control ctrl)
        {
            ctrl.Init();
            if (col < 0 || col >= this.__columns)
                throw new RangeError("col not valid");
            this.__domNode.GetChildren()[col].AppendChild(ctrl.DomNode);
            this._colctrls[col].Push(ctrl);
        }

        public void RemoveControl(uint col, Control ctrl)
        {
            if (col < 0 || col >= this.__columns)
                throw new RangeError("col not valid");
            this.__domNode.GetChildren()[col].RemoveChild(ctrl.DomNode);
            this._colctrls[col].Remove(ctrl);
        }

        public override void ClearControls()
        {
            for (var i = 0; i < this.__domNode.GetChildren().Length; i++)
            {
                var dom = this.__domNode.GetChildren()[i];
                for (var j = 0; j < this._colctrls[i].Length; j++)
                {
                    dom.RemoveChild(this._colctrls[i][j].DomNode);
                }
                this._colctrls[i] = new Control[0];
            }
        }

        public void InsertControlAt(uint col, Control ctrl, uint idx)
        {
            if (col < 0 || col >= this.__columns)
                throw new RangeError("col not valid");

            this.__domNode.GetChildren()[col].InsertChildAt(ctrl.DomNode, idx);
            this._colctrls[col].InsertAt(ctrl, idx);
        }

        public override void Render(Size sz)
        {
            for (var i = 0; i < this.__columns; i++)
            {
                var container = this.__domNode.GetChildren()[i];
                container.Height = sz.Height;
                var size = new RexToy.UI.Size(container.Width, container.Height);
                var h = sz.Height;
                Control resizeCtrl = null;
                for (var j = 0; j < this._colctrls[i].Length; j++)
                {
                    var ctrl = this._colctrls[i][j];
                    var ctn = ctrl as Container;
                    if (ctrl.IsContainer && ctn.SizeUpdate)
                    {
                        if (resizeCtrl != null)
                            throw new Exception("container can have only 1 resizable control!");
                        else
                            resizeCtrl = ctrl;
                    }
                    else
                    {
                        h -= ctrl.GetSize().Height;
                        if (ctrl.IsContainer)
                            ctrl.Render(ctrl.GetSize());
                    }
                }

                if (resizeCtrl != null)
                {
                    resizeCtrl.DomNode.Height = h;
                    resizeCtrl.Render(new RexToy.UI.Size(container.Width, h));
                }
            }
        }
    }
}
